package com.qf.fmall2302.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall2302.product.entity.Product;
import com.qf.fmall2302.product.entity.ProductImg;
import com.qf.fmall2302.product.entity.ProductSku;
import com.qf.fmall2302.product.mapper.ProductImgMapper;
import com.qf.fmall2302.product.mapper.ProductMapper;
import com.qf.fmall2302.product.mapper.ProductSkuMapper;
import com.qf.fmall2302.product.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 服务实现类
 * </p>
 *
 * @author dongjl
 * @since 2023-08-23
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {


    @Autowired
    ProductImgMapper productImgMapper;

    @Autowired
    ProductSkuMapper productSkuMapper;

    @Override
    public List<Product> listRecommends() {

        //1. 最新上架的三个商品信息
        QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>()
                .orderByDesc("create_time")
                .last("limit 3");
        List<Product> productList = this.list(queryWrapper);

        //2. 查询商品图片信息
        addPic(productList);

        return productList;
    }

    @Override
    public List<Product> listRecommendsBysql() {
        return null;
    }

    public void addPic(List<Product> productList) {

        // List<Product>  --->  List<商品ID>
        List<String> idList = productList.stream().map(Product::getProductId).collect(Collectors.toList());

        //1. SELECT * FROM product_img WHERE item_id IN (1,2,3);
        QueryWrapper<ProductImg> queryWrapper = new QueryWrapper<ProductImg>().in("item_id", idList);
        List<ProductImg> productImgs = productImgMapper.selectList(queryWrapper);

        //2. List<ProductImg> ---->  Map<商品id,List<图片>>
        Map<String, List<ProductImg>> imgMap = productImgs.stream()
                .collect(Collectors.groupingBy(ProductImg::getItemId));

        //3. 遍历 productList，挂图片
        for (Product product : productList) {
            String productId = product.getProductId();
            List<ProductImg> imgs = imgMap.get(productId);
            if (imgs == null){
                product.setImgs(new ArrayList<>());
            }else {
                product.setImgs(imgs);
            }
        }

    }

    @Override
    public HashMap<String, Object> detailInfo(String productid) {

        //1. 查询商品基本信息
        Product product = this.getById(productid);

        //2. 查询商品售卖套餐信息
        List<ProductSku> productSkuList = productSkuMapper.selectList(new QueryWrapper<ProductSku>().eq("product_id", productid));

        //3. 查询商品图片信息
        List<ProductImg> productImgs = productImgMapper.selectList(new QueryWrapper<ProductImg>().eq("item_id", productid));

        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("product",product);
        hashMap.put("productSkus",productSkuList);
        hashMap.put("productImgs",productImgs);

        return hashMap;
    }


}
